package com.southminority.ethnic.controller.news;

import com.southminority.ethnic.common.result.PageResult;
import com.southminority.ethnic.common.result.R;
import com.southminority.ethnic.service.news.NewsService;
import com.southminority.ethnic.controller.news.vo.NewsVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 新闻资讯展示控制器
 * 
 * @author SouthMinority
 */
@RestController
@RequestMapping("/api/news")
@Tag(name = "10 - 新闻资讯展示API", description = "【前台】新闻、公告、研究进展等信息的查询与展示接口")
public class NewsController {

    private final NewsService newsService;

    public NewsController(NewsService newsService) {
        this.newsService = newsService;
    }

    @GetMapping("/page")
    @Operation(summary = "分页和条件查询已发布的新闻列表", description = "用于前端新闻中心、通知公告等列表页面，支持按关键词、分类和类型筛选。")
    @ApiResponses(value = {
            @ApiResponse(responseCode = "200", description = "查询成功"),
            @ApiResponse(responseCode = "500", description = "查询失败")
    })
    public R<PageResult<NewsVO>> getPublishedNewsPage(
            @Parameter(description = "当前页码", example = "1") @RequestParam(defaultValue = "1") Integer page,
            @Parameter(description = "每页显示数量", example = "10") @RequestParam(defaultValue = "10") Integer size,
            @Parameter(description = "搜索关键词", example = "研讨会") @RequestParam(required = false) String keyword,
            @Parameter(description = "分类", example = "学术会议") @RequestParam(required = false) String category,
            @Parameter(description = "新闻类型", example = "news", schema = @Schema(allowableValues = {"news", "progress", "announcement"})) @RequestParam(required = false) String newsType,
            @Parameter(description = "归属机构ID", example = "1") @RequestParam(required = false) Long organizationId) {
        try {
            PageResult<NewsVO> pageResult = newsService.getPublishedNewsPage(page, size, keyword, category, newsType, organizationId);
            return R.ok(pageResult);
        } catch (Exception e) {
            return R.fail("查询新闻列表失败: " + e.getMessage());
        }
    }

    @GetMapping("/detail/{id}")
    @Operation(summary = "获取单篇新闻的详细信息", description = "根据新闻ID查询，返回其完整内容。调用此接口会增加该新闻的浏览次数。")
    @ApiResponses(value = {
            @ApiResponse(responseCode = "200", description = "获取成功"),
            @ApiResponse(responseCode = "404", description = "新闻不存在或未发布"),
            @ApiResponse(responseCode = "500", description = "服务器内部错误")
    })
    public R<NewsVO> getNewsDetail(@Parameter(description = "新闻的唯一ID", example = "1", required = true) @PathVariable Long id) {
        NewsVO newsVO = newsService.getPublishedNewsDetail(id);
        return R.ok(newsVO);
    }
    
    @GetMapping("/top")
    @Operation(summary = "获取置顶新闻列表", description = "用于首页轮播图或重要新闻区域，默认返回最多5条。")
    @ApiResponses(value = {
            @ApiResponse(responseCode = "200", description = "获取成功"),
            @ApiResponse(responseCode = "500", description = "服务器内部错误")
    })
    public R<List<NewsVO>> getTopNewsList(
            @Parameter(description = "需要获取的置顶新闻数量", example = "5") @RequestParam(defaultValue = "5") Integer limit) {
        try {
            List<NewsVO> bannerList = newsService.getTopNewsList(limit);
            return R.ok(bannerList);
        } catch (Exception e) {
            return R.fail("获取置顶新闻失败: " + e.getMessage());
        }
    }

    @GetMapping("/categories")
    @Operation(summary = "获取所有新闻分类列表", description = "用于前端筛选菜单，返回一个包含所有不重复分类名称的字符串列表。")
    @ApiResponses(value = {
            @ApiResponse(responseCode = "200", description = "获取成功"),
            @ApiResponse(responseCode = "500", description = "服务器内部错误")
    })
    public R<List<String>> getNewsCategories() {
        try {
            List<String> categories = newsService.getNewsCategories();
            return R.ok(categories);
        } catch (Exception e) {
            return R.fail("获取新闻分类失败: " + e.getMessage());
        }
    }

    @GetMapping("/test")
    @Operation(summary = "测试接口连通性", description = "一个简单的GET请求，用于检查服务是否正常运行。")
    @ApiResponses(value = {
            @ApiResponse(responseCode = "200", description = "服务正常")
    })
    public R<String> test() {
        return R.ok("新闻资讯前端服务正常");
    }
} 
